/**
 * Copyright (c) 2012, SHX Studio.
 * All rights reserved.
 * rosary226@gmail.com
 * 
 * @author shenqianfeng
 * @date 2012-9-1  
 * @version 1.0   
 */

package com.xxx.sqfengine.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class SQFGLBufferUtil {
	
	public static final int BYTES_PER_FLOAT = 4; 
	public static final int BYTES_PER_SHORT = 2; 
	
	public static FloatBuffer toFloatBuf(float [] floatBuf) {
		// a float is 4 bytes, therefore we multiply the number if
		// vertices with 4.
		ByteBuffer vbb = ByteBuffer.allocateDirect(floatBuf.length * BYTES_PER_FLOAT);
		vbb.order(ByteOrder.nativeOrder());
		FloatBuffer floatBuffer = vbb.asFloatBuffer();
		floatBuffer.put(floatBuf);
		floatBuffer.position(0);
		//floatBuffer.limit(floatBuf.length);
		return floatBuffer;
	}
	
	public static ShortBuffer toShortBuf(short [] shortBuf) {
		// short is 2 bytes, therefore we multiply the number if
		// vertices with 2.
		ByteBuffer ibb = ByteBuffer.allocateDirect(shortBuf.length * BYTES_PER_SHORT);
		ibb.order(ByteOrder.nativeOrder());
		ShortBuffer shortBuffer = ibb.asShortBuffer();
		shortBuffer.put(shortBuf);
		shortBuffer.position(0);
		shortBuffer.limit(shortBuf.length);
		return shortBuffer;
	}
	
	public static FloatBuffer toFloatBuffer4(float a, float b, float c, float d)
	{
		ByteBuffer buf = ByteBuffer.allocateDirect(4 * BYTES_PER_FLOAT);
		buf.order(ByteOrder.nativeOrder());
		FloatBuffer buffer = buf.asFloatBuffer();
		buffer.put(a);
		buffer.put(b);
		buffer.put(c);
		buffer.put(d);
		buffer.position(0);
		return buffer;
	}
}
